package com.union.core.organization.action;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.tuckey.web.filters.urlrewrite.utils.URLDecoder;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.union.core.doc.model.DocTemplate;
import com.union.core.doc.model.OfficialDoc;
import com.union.core.doc.model.OfficialDocOrg;
import com.union.core.doc.model.SealSignature;
import com.union.core.doc.pojo.OfficialDocOrgPojo;
import com.union.core.doc.pojo.OfficialDocPojo;
import com.union.core.doc.service.OfficialDocManagerService;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.organization.model.OrganizationParam;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.OrganizationManagerService;
import com.union.core.organization.service.OrganizationParamService;
import com.union.core.organization.service.PersonnelService;

import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;

public class OrganizationManagerAction extends ActionSupport{
	
	private static final long serialVersionUID = 872316012305159L;
	private SysService sysService = SysService.getInstance();
	
	private static Logger log = Logger.getLogger(OrganizationManagerAction.class.getName());
	
	private OrganizationManagerService organizationService;
	private OrganizationManager organization;
	private OrganizationManager organization1;
	private OrganizationManager organization2;
	private List<OrganizationManager> OrganizationList;
	
	private OrganizationParamService paramService;
	private OrganizationParam param;
	
	private PersonnelService personnelService;
	private Personnel personnel;
	
	//分页
	private int start;
	private int intPageSize =15; //每页行数
	private int page = 1;      //页码
	private int intRowCount;   //总行数
	private int intPageCount;  //总页数
	private int intPage;       //页数变量
	
	//机构信息
	private int organization_id;               //机构id
	private String organization_name;          //机构名
	private String organization_parent;        //父机构名称
	private String organization_description;   //机构描述
	private String add_name;                   //添加人
	private String updae_name;                 //修改人
	private int grade;                         //机构等级
	private int pid =0;                        //父机构id
	private ByteArrayInputStream inputStream;
	
	private HttpServletResponse response ;
	//日志
	private SysLogsService logsSer;
	//权限控制
	private List<String> allPower;

	private OfficialDoc officialDoc;
	private OfficialDocOrg officialDocOrg;
	private OfficialDocPojo officialDocPojo;

	private List<SealSignature> sealSignatureList;
	private SealSignature sealSignature;
	private List<DocTemplate> docTemplateList;
	private DocTemplate docTemplate;
	
	private int f;
	
	public OrganizationManagerAction(){
		organizationService = new OrganizationManagerService();
		organization= new OrganizationManager();
		organization1= new OrganizationManager();
		organization2= new OrganizationManager();
		OrganizationList = new ArrayList<OrganizationManager>();
		paramService= new OrganizationParamService();
		param = new OrganizationParam();
		personnelService = new PersonnelService();
		personnel = new Personnel();
		logsSer = new SysLogsService();

		officialDoc= new OfficialDoc();
		officialDocOrg = new OfficialDocOrg();
		officialDocPojo = new OfficialDocPojo();
	}
	
	public String queryList1(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			organization = organizationService.query1(sysService.getSessionListener().getMANAGER_ID());
            if(organization==null){
            	pid=0;
            	lookup();
            }
				return "SUCCESS";
		}	
	}
	
	public String queryList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			allPower=sysService.getSessionListener().getAllPower();
				lookup();
				setQuerySession();

				return "SUCCESS";
		}	
	}
	
	public String queryReturnList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			
			organization2 = organizationService.query1(sysService.getSessionListener().getMANAGER_ID());
			if(organization2!=null){
				if(organization2.getOrganization_id()==pid){
					organization = organizationService.query(pid);
					f = 1;
				}else{
					organization1 = organizationService.query(pid);
					pid = organization1.getPid();
						lookup();
						setQuerySession();

				}	
			}else{
				organization1 = organizationService.query(pid);
				pid = organization1.getPid();
					lookup();
					setQuerySession();
			}
			
				return "SUCCESS";
		}	
	}
	
	/**
	 * 查询列表(多页)
	 * @return String
	 */
	public String pagelist(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			getQuerySession();  //get the query sessions before lookup!
			lookup();
			return "SUCCESS";
		}
	}
	
	public String tree(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
				return "SUCCESS";
		}	
	}
	
	/*
	 * 加载添加机构
	 */
	public String addload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			return "SUCCESS";
		}
	}
	
	/*
	 * 添加机构
	 */
	public String add(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			organization.setOrganization_name(organization_name);
			organization.setPid(pid);
			organization1 = organizationService.query(pid);
			organization.setOrganization_parent(organization1.getOrganization_name());
			organization.setOrganization_description(organization_description);
			organization.setAdd_name(sysService.getSessionListener().getMANAGER_NAME());
			organizationService.add(organization);
			
			organization = organizationService.query(pid,organization_name);
			param = paramService.query(pid);
			param.setGrade(param.getGrade()+1);
			param.setOrganization_id(organization.getOrganization_id());
			param.setOrganization_name(organization_name);

			
			/*if("海口市总工会".equals(organization.getOrganization_parent())){
				param.setGrade(2);
			}else{
				if("海口市总工会".equals(organization1.getOrganization_parent())){
					param.setGrade(3);
				}else{
					param.setGrade(4);
				}
			}*/
			param.setAdd_name(sysService.getSessionListener().getMANAGER_NAME());
			paramService.add(param);
			logsSer.addSysLogs("添加机构【"+organization_name+"】");
			return "SUCCESS";
		}
	}
	
	/*
	 * 加载修改机构
	 */
	public String editload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			organization = organizationService.query(organization_id);
			return "SUCCESS";
		}
	}
	
	/*
	 * 查询机构信息
	 */
	public String query(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			organization = organizationService.query(organization_id);
			return "SUCCESS";
		}
	}
	
	/*
	 * 修改机构信息
	 */
	public String edit(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			System.out.println("bbbbbbbb"+pid);
			
			organization.setPid(pid);
			organization1 = organizationService.query(pid);
			System.out.println("cccccc"+organization1);
			if(organization1!=null){
				if(organization1.getOrganization_name()!=null && organization1.getOrganization_name()!=""){
					organization.setOrganization_parent(organization1.getOrganization_name());
				}
			}
			
			
			organization.setOrganization_name(organization_name);
			organization.setOrganization_description(organization_description);
			organization.setUpdae_name(sysService.getSessionListener().getMANAGER_NAME());
			organizationService.edit(organization);
			
			param.setOrganization_name(organization_name);
			/*organization = organizationService.query(pid,organization_name);
			if("海口市总工会".equals(organization.getOrganization_name())){
				param.setGrade(1);
			}
			if("海口市总工会".equals(organization.getOrganization_parent())){
				param.setGrade(2);
			}else{
				if("海口市总工会".equals(organization1.getOrganization_parent())){
					param.setGrade(3);
				}else{
					param.setGrade(4);
				}
			}*/
			
			param.setUpdae_name(sysService.getSessionListener().getMANAGER_NAME());
			paramService.edit(param);
			logsSer.addSysLogs("修改机构【"+organization_name+"】");
			return "SUCCESS";
		}
	}
	
	/*
	 * 删除机构信息
	 */
	public String delete(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			organization=organizationService.query(organization_id);
			organizationService.delete(organization_id);
			paramService.delete(organization_id);
			personnelService.deleteById(organization_id);
			logsSer.addSysLogs("删除机构【"+organization.getOrganization_name()+"】");
			return "SUCCESS";
		}
	}
	
	@JSON
	public String queryByZoneunion(){
		OrganizationList = organizationService.queryList(organization_id);
		return Action.SUCCESS;
	}
	
	public String checkPid(){
		Boolean canSign = false;
		boolean result = organizationService.checkPid(pid);
    	if(result==true){//存在,要返回true
			canSign = false;
		}else{
			canSign = true;
		}
		inputStream = new ByteArrayInputStream(canSign.toString().getBytes());
        return SUCCESS;
	}
	
	public String checkOrganizationName(){
		Boolean canSign = false;
		boolean result = organizationService.checkOrganizationName(organization_name);
		if(result==true){//存在,要返回true
			canSign = false;
		}else{
			canSign = true;
		}
		inputStream = new ByteArrayInputStream(canSign.toString().getBytes());
        return SUCCESS;
	}
	
	private void lookup(){
	
		if(organization!=null){
			System.out.println("xxxx"+organization.getOrganization_id());
			organization.equals("");
			System.out.println("zzzz"+organization.getOrganization_id());
		}
		if("".equals(pid))pid=0;
		
		if(page<1){
			page = 1;
			intPage = 1;
		}else{
			intPage = page;
		}
		intRowCount = organizationService.ListCount(pid);
		intPageCount = (intRowCount + intPageSize -1)/intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			OrganizationList = organizationService.query(pid, start, intPageSize);
		}	
	}
	
	private void setQuerySession(){
		ServletActionContext.getRequest().getSession().setAttribute("pid",pid); 
	}
	
	private void getQuerySession(){
		pid = (Integer)ServletActionContext.getRequest().getSession().getAttribute("pid");
	}

	public OrganizationManagerService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(
			OrganizationManagerService organizationService) {
		this.organizationService = organizationService;
	}

	public List<OrganizationManager> getOrganizationList() {
		return OrganizationList;
	}

	public void setOrganizationList(List<OrganizationManager> organizationList) {
		OrganizationList = organizationList;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getIntPageSize() {
		return intPageSize;
	}

	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getIntRowCount() {
		return intRowCount;
	}

	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}

	public int getIntPageCount() {
		return intPageCount;
	}

	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public int getOrganization_id() {
		return organization_id;
	}

	public void setOrganization_id(int organizationId) {
		organization_id = organizationId;
	}

	public String getOrganization_name() {
		return organization_name;
	}

	public void setOrganization_name(String organizationName) {
		organization_name = organizationName;
	}

	public String getOrganization_parent() {
		return organization_parent;
	}

	public void setOrganization_parent(String organizationParent) {
		organization_parent = organizationParent;
	}

	public String getOrganization_description() {
		return organization_description;
	}

	public void setOrganization_description(String organizationDescription) {
		organization_description = organizationDescription;
	}

	public String getAdd_name() {
		return add_name;
	}

	public void setAdd_name(String addName) {
		add_name = addName;
	}

	public String getUpdae_name() {
		return updae_name;
	}

	public void setUpdae_name(String updaeName) {
		updae_name = updaeName;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public OrganizationManager getOrganization1() {
		return organization1;
	}

	public void setOrganization1(OrganizationManager organization1) {
		this.organization1 = organization1;
	}

	public OrganizationParam getParam() {
		return param;
	}

	public void setParam(OrganizationParam param) {
		this.param = param;
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		OrganizationManagerAction.log = log;
	}

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Personnel getPersonnel() {
		return personnel;
	}

	public void setPersonnel(Personnel personnel) {
		this.personnel = personnel;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public OfficialDoc getOfficialDoc() {
		return officialDoc;
	}

	public void setOfficialDoc(OfficialDoc officialDoc) {
		this.officialDoc = officialDoc;
	}

	public OfficialDocOrg getOfficialDocOrg() {
		return officialDocOrg;
	}

	public void setOfficialDocOrg(OfficialDocOrg officialDocOrg) {
		this.officialDocOrg = officialDocOrg;
	}

	public OfficialDocPojo getOfficialDocPojo() {
		return officialDocPojo;
	}

	public void setOfficialDocPojo(OfficialDocPojo officialDocPojo) {
		this.officialDocPojo = officialDocPojo;
	}

	public List<SealSignature> getSealSignatureList() {
		return sealSignatureList;
	}

	public void setSealSignatureList(List<SealSignature> sealSignatureList) {
		this.sealSignatureList = sealSignatureList;
	}

	public SealSignature getSealSignature() {
		return sealSignature;
	}

	public void setSealSignature(SealSignature sealSignature) {
		this.sealSignature = sealSignature;
	}

	public List<DocTemplate> getDocTemplateList() {
		return docTemplateList;
	}

	public void setDocTemplateList(List<DocTemplate> docTemplateList) {
		this.docTemplateList = docTemplateList;
	}

	public DocTemplate getDocTemplate() {
		return docTemplate;
	}

	public void setDocTemplate(DocTemplate docTemplate) {
		this.docTemplate = docTemplate;
	}
	public List<String> getAllPower() {
		return allPower;
	}

	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}

	public OrganizationManager getOrganization() {
		return organization;
	}

	public void setOrganization(OrganizationManager organization) {
		this.organization = organization;
	}

	public OrganizationManager getOrganization2() {
		return organization2;
	}

	public void setOrganization2(OrganizationManager organization2) {
		this.organization2 = organization2;
	}

	public int getF() {
		return f;
	}

	public void setF(int f) {
		this.f = f;
	}
}